﻿using Newtonsoft.Json;
using Senparc.Weixin.MP.Containers;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.Helpers;
using Senparc.Weixin.MP.TenPayLibV3;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using ZBJF.Warranty.Common.Common;
namespace ZBJF.Warranty.WeiXinPayAPI
{
    public class JsApiPay
    {
        private static TenPayV3Info tenPayV3Info = new TenPayV3Info(WxPayConfig.APPID,
                                                                    WxPayConfig.APPSECRET,
                                                                    WxPayConfig.MCHID,
                                                                    WxPayConfig.KEY,
                                                                    WxPayConfig.NOTIFY_URL);//通知地址

        #region 微信预支付 + UnifiedorderResult PayInfo(string attach, string body, string openid, int price, string orderNum)
        /// <summary>
        /// 微信预支付
        /// </summary>
        /// <param name="body">商品或支付单简要描述</param>
        /// <param name="openid">用户在商户公众号appid下的唯一标识(非必填)</param>
        /// <param name="price">订单总金额，单位为分</param>
        /// <param name="orderNum">订单号</param>
        /// <param name="attach">附加数据，在查询API和支付通知中原样返回，可作为自定义参数使用。</param>
        /// <returns>微信支付结果（result_code和return_code 皆=“SUCCESS”时表示预支付成功）</returns>
        public static UnifiedorderResult PrePay(string body, string openid, int price, string orderNum, string attach = null)
        {
            TenPayV3UnifiedorderRequestData data = new TenPayV3UnifiedorderRequestData(tenPayV3Info.AppId, tenPayV3Info.MchId, body, orderNum, price, "127.0.0.1", tenPayV3Info.TenPayV3Notify, Senparc.Weixin.MP.TenPayV3Type.JSAPI, openid, tenPayV3Info.Key, Senparc.Weixin.MP.TenPayLib.TenPayUtil.GetNoncestr());
            data.Attach = attach;
            try
            {
                //获取并返回预支付XML信息
                UnifiedorderResult result = TenPayV3.Unifiedorder(data);
                return result;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex.ToString(), LogType.error);
                return null;
            }
        }
        #endregion

        #region 获取支付实体 + PayParamsModel GetPayModel(string prePay_id)
        /// <summary>
        /// 获取支付实体
        /// </summary>
        /// <param name="prePay_id">预支付ID</param>
        /// <returns></returns>
        public static PayParamsModel GetPayModel(string prePay_id)
        {
            PayParamsModel model = new PayParamsModel(tenPayV3Info.AppId, prePay_id);
            model.NonceStr = Guid.NewGuid().ToString().Replace("-", "");

            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            model.TimeStamp = Convert.ToInt64(ts.TotalSeconds).ToString();
            //model.PaySign = TenPayV3.GetJsPaySign(model.AppID, model.TimeStamp, model.NonceStr, model.Package, tenPayV3Info.Key);
            WxPayData wxpay = new WxPayData();
            wxpay.SetValue("appId", model.AppID);
            wxpay.SetValue("timeStamp", model.TimeStamp);
            wxpay.SetValue("nonceStr", model.NonceStr);
            wxpay.SetValue("package", model.Package);
            wxpay.SetValue("signType", "MD5");
            model.PaySign = wxpay.MakeSign();
            //wxpay.SetValue("appId", "wx4d4d82224876756f");
            //wxpay.SetValue("timeStamp", "1513864609");
            //wxpay.SetValue("nonceStr", "8467f95b16bc47048864ac9095fd4bb9");
            //wxpay.SetValue("package", "prepay_id=wx20171221215649a6cb7169ed0056644550");
            //wxpay.SetValue("signType", "MD5");
            //model.PaySign = wxpay.MakeSign();
            LogHelper.WriteLog("生成签名" + model.AppID + "——" + model.TimeStamp + "——" + model.NonceStr + "——" + model.Package + "商户密钥" + tenPayV3Info.Key + "；结果：" + model.PaySign, LogType.remark);
            return model;
        }
        #endregion

    }
}
